<template>
	<coreshop-page title="提货单核销" mode="left">
		<view class="p-25">
			<view class="search-box">
				<uv-search v-model="state.id" :showAction="true" actionText="搜索" @custom="handleSearch"></uv-search>
			</view>
			<view class="goods-box" v-if="state.list.length > 0">
				<view class="item-box" v-for="item in state.list" :key="item.id">
					<view class="order-num">
						<view class="id">
							<text class="tit">订单号：{{item.orderId}} </text>
						</view>
						<view class="status">{{item.statusName}}</view>
					</view>
					<view class="goods">
						<view class="item" v-for="itemChild in item.orderItems" :key="itemChild.id">
							<coreshop-goods-card type="horizontal" imgHeight="200rpx" imgWidth="200rpx"
								:goodsData="hanldeCombinationGoodsData(itemChild)">
								<template #goodPrice>
									<view class="goods-msg">
										<view class="price-box">
											<view class="price">￥{{itemChild.price}}</view>
											<view class="num">数量：{{itemChild.nums}}</view>
										</view>
										<view class="time">
											<view>SN码：{{itemChild.sn}}</view>
											<view>BN码：{{itemChild.bn}}</view>
										</view>
									</view>
								</template>
							</coreshop-goods-card>
						</view>
					</view>
					<view class="btn-box">
						<view class="time-box">
							下单时间：{{timeFormat(item.createTime, 'mm-dd hh:MM:ss')}}
						</view>
						<view class="time-box" v-if="item.status">
							提货时间：{{timeFormat(item.pickUpTime, 'mm-dd hh:MM:ss')}}
						</view>
						<uv-checkbox-group v-if="!item.status" v-model="item.checkboxValue" :size="25"
							activeColor="#D33123" :customStyle="{'flex':'none'}">
							<uv-checkbox :name="item.id"></uv-checkbox>
						</uv-checkbox-group>
					</view>
				</view>
			</view>
			<view v-else class="layout-empty-box">
				<coreshop-empty :mode="EmptyEnum.data" text="暂无数据"></coreshop-empty>
			</view>

			<view class="qrcode" @click="handleQrcode">
				<uv-icon name="scan" color="#e54d42" size="40"></uv-icon>
			</view>

			<coreshop-fixed-bottom-button :radius="100" title="确认核销"
				@onClick="handleConfirm"></coreshop-fixed-bottom-button>
		</view>
	</coreshop-page>

</template>

<script setup lang="ts">
	import { reactive } from 'vue';
	import { onLoad } from '@dcloudio/uni-app';
	import { queryLadingInfo, queryLading } from '@/core/api';
	import type { Response } from '@/core/models';
	import { handleShowToast } from '@/core/utils';
	import { EmptyEnum } from '@/core/enum';
	import { timeFormat } from '@/uni_modules/uv-ui-tools/libs/function/index.js';

	interface QueryParams {
		id : string;
	}

	const state = reactive<{
		list : Array<any>;
		id : string;
	}>({
		list: [],
		id: '',
	})

	onLoad((query : QueryParams) => {
		state.id = query?.id || '';
		getLadingInfo();
	});


	/** 搜索 */
	const handleSearch = () => {
		if (!state.id) {
			handleShowToast("请输入核销码")
			return;
		}
		getLadingInfo();
	}

	const getLadingInfo = async () => {
		const ladingInfo : Response<any> = await queryLadingInfo({ id: state.id });
		if (ladingInfo.status) {
			state.list = ladingInfo.data.map((item : any) => {
				item['checkboxValue'] = [`${item.id}`];
				return item;
			});
		}
	}

	/** 组合商品数据传入组件 */
	const hanldeCombinationGoodsData = (item : any) => {
		return {
			id: item.id,
			image: item.imageUrl,
			name: item.name,
			brief: item.addon,
		}
	}

	/** 确认核销 */
	const handleConfirm = async () => {
		let data : any = state.list.find((item : any) => item.checkboxValue.length > 0);
		if (!data) {
			handleShowToast('请选择要核销的订单'); return;
		}
		const lading : Response<any> = await queryLading({
			id: data.checkboxValue[0],
		});
		if (lading.status) {
			handleShowToast(lading.msg, 'success', () => {
				getLadingInfo();
			});
		} else {
			handleShowToast(lading.msg);
		}
	}

	/** 扫一扫 */
	const handleQrcode = () => {
		uni.scanCode({
			onlyFromCamera: true,
			success: function (res : any) {
				state.id = res.result;
				getLadingInfo();
			}
		});
	}
</script>

<style scoped lang="scss">
	@import './index.scss';
</style>